const { pool } = require('./app');

// 插入测试数据
const insertTestData = async () => {
  const conn = await pool.getConnection();
  try {
    // 插入测试歌曲
    const testSongs = [
      {
        title: '夜空中最亮的星',
        artist: '逃跑计划',
        lyrics: '夜空中最亮的星 能否听清\n那仰望的人 心底的孤独和叹息\n夜空中最亮的星 能否记起\n曾与我同行 消失在风里的身影',
        song_url: 'https://music.163.com/song/media/outer/url?id=25706282.mp3',
        cover_url: 'https://ai-public.mastergo.com/ai/img_res/647cde825adf712e96b3bc66cb67656c.jpg',
        duration: 240
      },
      {
        title: '海阔天空',
        artist: 'Beyond',
        lyrics: '今天我 寒夜里看雪飘过\n怀着冷却了的心窝漂远方\n风雨里追赶 雾里分不清影踪\n天空海阔你与我 可会变',
        song_url: 'https://music.163.com/song/media/outer/url?id=346576.mp3',
        cover_url: 'https://ai-public.mastergo.com/ai/img_res/c259606553810742eff43369376a2a87.jpg',
        duration: 320
      },
      {
        title: '起风了',
        artist: '买辣椒也用券',
        lyrics: '这一路上走走停停\n顺着少年漂流的痕迹\n迈出车站的前一刻\n竟有些犹豫',
        song_url: 'https://music.163.com/song/media/outer/url?id=1330348068.mp3',
        cover_url: 'https://ai-public.mastergo.com/ai/img_res/5664d87772b76472ab0cd4f1d1a44d1d.jpg',
        duration: 280
      },
      {
        title: '稻香',
        artist: '周杰伦',
        lyrics: '对这个世界如果你有太多的抱怨\n跌倒了就不敢继续往前走\n为什么人要这么的脆弱 堕落\n请你打开电视看看',
        song_url: 'https://music.163.com/song/media/outer/url?id=185908.mp3',
        cover_url: 'https://ai-public.mastergo.com/ai/img_res/dceca67b9c576b6031ddcfce7b27afb8.jpg',
        duration: 220
      },
      {
        title: '晴天',
        artist: '周杰伦',
        lyrics: '故事的小黄花\n从出生那年就飘着\n童年的荡秋千\n随记忆一直晃到现在',
        song_url: 'https://music.163.com/song/media/outer/url?id=186016.mp3',
        cover_url: 'https://ai-public.mastergo.com/ai/img_res/0d326ebc29d14771873128d2e0f564fd.jpg',
        duration: 260
      }
    ];

    for (const song of testSongs) {
      await conn.execute(
        'INSERT INTO songs (title, artist, lyrics, song_url, cover_url, duration) VALUES (?, ?, ?, ?, ?, ?)',
        [song.title, song.artist, song.lyrics, song.song_url, song.cover_url, song.duration]
      );
    }

    // 插入测试播放列表
    const testPlaylists = [
      {
        name: '经典华语',
        description: '经典华语歌曲合集',
        cover_url: 'https://ai-public.mastergo.com/ai/img_res/dceca67b9c576b6031ddcfce7b27afb8.jpg',
        is_public: true
      },
      {
        name: '我的收藏',
        description: '个人收藏的歌曲',
        cover_url: 'https://ai-public.mastergo.com/ai/img_res/647cde825adf712e96b3bc66cb67656c.jpg',
        is_public: false
      }
    ];

    for (const playlist of testPlaylists) {
      await conn.execute(
        'INSERT INTO playlists (name, description, cover_url, is_public) VALUES (?, ?, ?, ?)',
        [playlist.name, playlist.description, playlist.cover_url, playlist.is_public]
      );
    }

    // 插入一些收藏记录
    const favorites = [
      { user_id: 'user_001', song_id: 1 },
      { user_id: 'user_001', song_id: 2 },
      { user_id: 'user_001', song_id: 3 }
    ];

    for (const favorite of favorites) {
      await conn.execute(
        'INSERT INTO user_favorites (user_id, song_id) VALUES (?, ?)',
        [favorite.user_id, favorite.song_id]
      );
    }

    // 插入一些播放历史
    const history = [
      { user_id: 'user_001', song_id: 1 },
      { user_id: 'user_001', song_id: 2 },
      { user_id: 'user_001', song_id: 3 },
      { user_id: 'user_001', song_id: 4 }
    ];

    for (const record of history) {
      await conn.execute(
        'INSERT INTO play_history (user_id, song_id) VALUES (?, ?)',
        [record.user_id, record.song_id]
      );
    }

    console.log('测试数据插入成功！');
  } catch (error) {
    console.error('插入测试数据失败:', error);
  } finally {
    conn.release();
    process.exit(0);
  }
};

// 运行脚本
insertTestData();
